<template>
	<view class="main">
		<!-- <view class="msg_list" @click="list(item.id)" v-for="(item,index) in list" :key="index">
		<view class="msg_list_tit"><text class="t22">{{item.title}}</text><text class="t1">{{item.addtime}}</text></view>
		</view> -->
		<view class="msg_list" @click="list(item.id)" v-for="(item,index) in orderlist" :key="index">
			<view class="msg_list_tit"><text class="t22">订单号：{{item.tradeId}}</text>
				<!-- <text class="t1">{{item.introduction}}创建时间</text> -->
			</view>
			<view class="msg_list_time t14" v-if="item.status==2">已付款</view>
			<view class="msg_list_tit"><text class="t22">商家 ：{{item.mchName?item.mchName:'普通用户'}}</text>
			</view>
			<view class="msg_list_tit"><text class="t22">联系电话 ：{{item.phone}}</text>
			</view>
			<view class="msg_list_tit"><text class="t22">地址 ：{{item.mchAddress}}</text>
			</view>
			<view class="msg_list_tit"><text class="t22">商品 ：{{item.name}}</text>
			</view>
			<view class="msg_list_tit"><text class="t22">快递单号 ：{{item.trackingNo}}</text>
			</view>
			<view class="msg_list_tit">
				<text class="t1">创建时间 ：{{item.createdTime}}</text>
			</view>
			<view class="msg_list_tit">
				<text class="t1">订单状态 ：{{getStatus(item.status)}}</text>
			</view>
			<view class="">
				<view class="hexiao t3" v-if="item.status==6" @click="received(item.tradeId,item.trackingNo)">确认收货</view>
				<view class="hexiao t3" v-if="item.status==2" @click="heixiao(item.picUrl)">查看核销码</view>
				<view class="hexiao t3" style="margin-left: 15upx;" v-if="item.status==2" @click="cancelOrder(item.tradeId)">取消订单</view>
			</view>
		</view>

		<view class="zanwu" v-show="jiazai" style="padding-top: 4vw;"><text class="t7">{{more}}</text></view>
		<view class="bai"></view>
		<view class="zanwu" v-show="zanwu">
			<image src="../../static/img/kong.png" mode="widthFix"></image><text class="t8">{{wu}}</text>
		</view>
		<!-- <view class="msg_list" @click="list(1)">
		<view class="msg_list_tit"><text class="t22">公告标题</text><text class="t1">公告标详情公告标详情公告标详情公告标详情公告标详情</text></view>
		<view class="msg_list_time t14">2020<br>04/25</view>
		</view> -->
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pid: '',
				orderlist: [],
				pages: 1,
				mainid: '',
				ismore: true,
				more: '加载更多',
				jiazai: false,
				wu: "暂无数据",
				zanwu: false,
			}
		},
		// 监听触底
		onReachBottom() {
			//阻止重复加载
			if (this.ismore == false) {
				return;
			}
			this.pages++;
			this.orlist();

		},

		onLoad(options) {
			var _this = this;
			_this.pid = options.id;
			uni.getStorage({
				key: 'user',
				success: function(res) {
					_this.mainid = res.data.id;
					_this.orlist();
				}
			});
		},
		methods: {
			
			cancelOrder(id){
				console.log("id",id);
				uni.request({
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
					},
					url: getApp().globalData.url + 'user/cancelAdSellLog/' + id, //仅为示例，并非真实接口地址。
					method: 'POST',
					data: {
						id: id, //	用户ID（mainId）		true	string
					},
					success: (res) => {
						console.log("res",res);
						if (res.data.status == '200') {
							uni.showToast({
								title: '订单取消成功,正在退款中',
								icon: 'none',
								duration: 2000
							});
							this.orlist()
						}else{
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						console.log("error:",error)
					}
				});
				
			},

			list(e) {
				// console.log(e);
				uni.navigateTo({
					url: '/pages/order/order?id=' + e
				});
			},
			orlist() {
				console.log('this.mainid:'+this.mainid);
				uni.request({
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
					},
					url: getApp().globalData.url + 'user/myAdSellListPage/' + this.mainid + '/' + this
						.pages, //仅为示例，并非真实接口地址。
					method: 'POST',
					data: {
						id: this.mainid, //	用户ID（mainId）		true	string
						page: this.pages, //
					},
					success: (res) => {
						uni.hideLoading(); //关闭加载动画
						console.log(res);
						var len = res.data.data.pages;
						console.log("len",len);
						if (res.data.status == '200') {
							if (len == this.pages) {
								this.more = "没有更多数据了";
								this.jiazai = true;
								this.zanwu = false;
								this.ismore = false;
							} else if (len == 0) {
								// console.log(111);
								this.more = "暂无数据";
								this.jiazai = false;
								this.zanwu = true;
								this.ismore = false;
							} else {
								this.more = "加载更多";
								this.jiazai = true;
								this.zanwu = false;
								this.ismore = true;
							}
							// for (var i = 0; i < res.data.data.length; i++) {
							// 	res.data.data[i].createdTime = res.data.data[i].createdTime.split("T");
							// }
							if (this.pages == 1) {

								this.orderlist = res.data.data.records;
							} else {
								this.orderlist = this.orderlist.concat(res.data.data.records);
							}
						}
					},
					fail: (error) => {
						uni.hideLoading();
						// this.checks = true
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
						console.log(error)
					}
				});
			},
			
			received(tradeId, trackingNo) {
				let data = {
					"trackingNo": trackingNo,
					"tradeId": tradeId
				}
				console.log('参数',data);
				uni.request({
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
					},
					url: getApp().globalData.url + 'ad/confirmShipping?trackingNo=' + trackingNo + "&tradeId=" + tradeId, //仅为示例，并非真实接口地址。
					method: 'POST',
					data: data,
					success: (res) => {
						uni.hideLoading(); //关闭加载动画
						console.log(res);
						if (res.status == 200) {
							this.orlist()
						}
					}
				});
				// common.request('ad/confirmShipping', data, res => {
				// 	console.log('确认收货：', res)
				// 	if (res.status == 200) {
				// 		this.orlist()
				// 	}
				// })
			},
			
			heixiao(url) {
				console.log('url:', url);
				let arr = []
				arr.push(url)
				uni.previewImage({
					content: 0,
					urls: arr,
					fail: (err) => {
						console.log("err: ", err);
					}
				})
			},
			
			getStatus(status) {
				let value = parseInt(status)
				switch (status) {
					case -1:
						return '已删除'
					case 0:
						return '已退款'
					case 1:
						return '已启用'
					case 2:
						return '已付款'
					case 3:
						return '结算中'
					case 4:
						return '已结算'
					case 5:
						return '待发货'
					case 6:
						return '已发货'
					case 7:
						return '退款中'
					default:
						break;
				}
			}
			
		}
	}
</script>

<style>
	.msg_list {
		width: 100%;
		height: auto;
		display: block;
		float: left;
		box-shadow: 0px 0px 2vw #ddd;
		padding: 2vw 3vw;
		border-radius: 2vw;
		border: 1px solid #F2F2F2;
		margin-top: 4vw;
	}

	.msg_list_tit {
		width: 70%;
		height: auto;
		display: block;
		float: left;
		padding: 1vw 0;
	}

	.msg_list_tit text {
		width: 100%;
		height: 10vw;
		line-height: 10vw;
		display: block;
		float: left;
		overflow: hidden;
	}

	.about_info {
		width: 100%;
		height: auto;
		display: block;
		float: left;
		line-height: 7vw;
	}

	.msg_list_time {
		width: 30%;
		height: auto;
		display: block;
		float: right;
		text-align: right;
		line-height: 10vw;
		margin-top: 1vw;
	}

	.imgs {
		width: 100%;
		height: auto;
		display: block;
		float: left;
	}
	.hexiao {
		width: auto;
		height: 7vw;
		line-height: 7vw;
		display: block;
		float: left;
		/* margin-left: 4vw; */
		border-radius: 2vw;
		background: #F1C75E;
		padding: 0 5vw;
	}
</style>
